<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button
          size="mini"
          type="primary"
          @click="$refs.addRole.showDialog=true"
        >添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRole.name" size="mini" />
            <span v-else> {{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="启用">
          <template slot-scope="{row}">
            <el-switch v-if="row.isEdit" v-model="row.editRole.state" :active-value="1" :inactive-value="0" />
            <span v-else>{{ row.state === 1 ? '启用':'禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRole.description" type="textarea" size="mini" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <template v-if="row.isEdit">
              <el-button type="primary" size="mini" @click="editRow(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit =false">取消</el-button>
            </template>
            <template v-else>
              <el-button @click="openAuth(row.id)" type="text" size="small">设置权限</el-button>
              <el-button type="text" size="small" @click="row.isEdit =true">编辑</el-button>&nbsp;
              <el-popconfirm
                title="确认删除吗?"
                confirm-button-type="primary"
                cancel-button-type="text"
                @onConfirm="delRow(row.id)"
              >
                <el-button slot="reference" size="small" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <!-- 3个变量  2个事件 -->
        <el-pagination
          :current-page.sync="queryData.page"
          :page-sizes="[10, 20, 80, 100]"
          :page-size="queryData.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="sizeChange"
          @current-change="currentChange"
        >
          :pager-count="7">
        </el-pagination>

      </el-row>
    </div>
    <!--新增 -->
    <addRole ref="addRole" @updateList="initData" />
    <!-- 角色关联权限 -->
    <setAuth ref="setAuth" />
  </div>
</template>
<script>
import { delRoleApi, getRoleListApi, editRoleApi, getRoleInfoApi } from '@/api/role'
import addRole from './components/addRole.vue'
import setAuth from './components/setAuth.vue'
export default {
  name: 'Role',
  components: {
    addRole,setAuth
  },
  data() {
    return {
      // 表格数据
      list: [],
      // 查询参数
      queryData: {
        page: 1,
        pagesize: 10
      },
      // 总数
      total: 0
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 获取角色列表
    async initData() {
      // 1.发送请求
      const res = await getRoleListApi(this.queryData)
      // 2. 数据加工处理
      // 3. 赋值变量
      this.list = res.rows
      this.list.forEach(item => {
        // item.isEdit = false // 错误的！数据不具备响应式
        this.$set(item, 'isEdit', false) // 正确的，数据具有响应式
        this.$set(item, 'editRole', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
      this.total = res.total
    },
    // 每页数量更换
    sizeChange(val) {
      this.queryData.pagesize = val
      this.initData()
    },
    // 页码更新事件
    currentChange(val) {
      this.queryData.page = val
      this.initData()
    },
    // 删除事件函数
    async delRow(id) {
      await delRoleApi(id)
      this.$message.success('删除成功')
      if (this.queryData.page > 1 && this.list.length === 1) {
        this.queryData.page--
      }
      this.initData()
    },
    // 修改确定事件
    async editRow(row) {
      // 校验数据
      if (!row.editRole.name || !row.editRole.description) return this.$message.warning('请输入名称或描述')
      // 修改请求
      await editRoleApi({ ...row.editRole, id: row.id })
      // 提示成功
      this.$message.success('操作成功')
      // 视图更新 (this.initData代价太大)
      //  Object.assign(目标对象,数据对象)
      Object.assign(row, { ...row.editRole, isEdit: false })
    },
    // 角色关联权限打开
    async openAuth(id){
      // 根据ID查询角色信息，将角色的权限信息赋给chechkAuth
      let res = await getRoleInfoApi(id)
      this.$refs.setAuth.roleId = id
      this.$refs.setAuth.chechkAuth = res.permIds
      this.$refs.setAuth.showDialog = true
    }
  }
}
</script>
<style scoped>
.app-container{
  padding: 30px;
}
.role-operate {
  padding: 10px;
}
</style>
